package com.lht.testDemo;

import org.junit.Test;

import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Stream;

/**
 * @author Eric
 * @version 1.0
 * @date 2019-07-19
 */
public class ReflectTest {


    @Test
    public void testClassType() {
        List<String> list = new ArrayList<>();
        /*
            isAssignableFrom( 子类class)  判断一个类，是否是一个接口或者父类的   实现类或者子类
         */
        System.out.println(Map.class.isAssignableFrom(HashMap.class));

        StringBuilder sb = new StringBuilder();
        sb.append("1");

        System.out.println(sb.toString());
    }

    @Test
    public void testGetFieldGetDeclareField() {
        Field[] fields = Car.class.getFields();
        Field[] fs = Car.class.getDeclaredFields();

        Stream.of(fields).forEach(k -> {
            System.out.print(k.getName());
        });
        System.out.println("--------declare-------------");


        Stream.of(fs).forEach(k -> {
            System.out.println(k.getName());
        });
        System.out.println("isAssignableFrom:" + Animal.class.isAssignableFrom(Car.class));
        fs = Car.class.getSuperclass().getDeclaredFields();
        System.out.println("父类属性");
        Stream.of(fs).forEach(k -> {
            System.out.println(k.getName());
        });
        System.out.println();
    }

    public class Animal {

        private String weight;

        public String getWeight() {
            return weight;
        }

        public void setWeight(String weight) {
            this.weight = weight;
        }
    }

    public class Car extends Animal {

        private String name;

        private String color;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }
    }

}
